﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace AnimatedSprites
{
    class Player: Sprite
    {
        public override Vector2 direction
        {
            get { return speed; }
        }
        // constructor
        public Player(Texture2D textureImage, Vector2 position,
            Point frameSize, int collisionOffset, Point currentFrame, Point sheetSize,
            Vector2 speed, InGameManager IGM)
            : base(textureImage, position, frameSize, collisionOffset, speed, null, 0, IGM)
        {
        }

        public override void Update()
        {
            //若能移动，则移动
            if (spriteManager.checkView(spriteManager.p1, spriteManager.p2))
                logicalPosition += speed;
        }

        //从Plays类接受四个输入值，改变速度，惯性
        public void speedChange(Vector2 direction){
            //direction是接收到的键盘（上下左右键或者wsad键）的输入
            //根据原始速度和输入速度计算在惯性下的新速度
            speed += direction;
            if (speed.X > maxSpeed.X)
                speed.X = maxSpeed.X;
            if (speed.X < (-1)*maxSpeed.X)
                speed.X = (-1)*maxSpeed.X;
            if (speed.Y > maxSpeed.Y)
                speed.Y = maxSpeed.Y;
            if(speed.Y < (-1)*maxSpeed.Y)
                speed.Y = (-1)*maxSpeed.Y;
        }

        //返回移动后的逻辑位置 unused
        public Vector2 getLogicalPositionAfterchange()
        {
            return logicalPosition + speed;
        }

        //将指定方向的速度变成0
        public void resetSpeed(Vector2 XorYZero){
            //XorYZero表示哪个方向的速度将要变成0
            if (XorYZero.X == 0)    speed.X = 0;
            if (XorYZero.Y == 0)    speed.Y = 0;
        }
    }
}
